import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as dotenv from 'dotenv';

// 确保在应用启动时加载环境变量
dotenv.config({
  path: '.env',
});

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 配置CORS
  app.enableCors({
    origin: true,
    credentials: true,
  });

  // 添加健康检查端点
  app.getHttpServer().on('request', (req: any, res: any) => {
    if (req.url === '/health') {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(
        JSON.stringify({ status: 'OK', timestamp: new Date().toISOString() }),
      );
    }
  });

  // 配置API前缀
  const globalPrefix = process.env.APP_PREFIX || 'api/v1';
  app.setGlobalPrefix(globalPrefix);

  const port = process.env.APP_PORT || 3000;
  await app.listen(port);
  console.log(
    `🚀 Application is running on: http://localhost:${port}/${globalPrefix}`,
  );
  console.log(`📚 API文档: 请参考控制器中的端点注释`);
  console.log(`🏥 Health check: http://localhost:${port}/health`);
}

bootstrap();
